<template>
  <t-dialog
    :visible.sync="visible"
    :header="`${header || ''}-上传批复文件`"
    width="30%"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    :on-confirm="confirmTip"
    :on-cancel="closeDialog"
  >
   <div style="display: flex;align-items: center;">
    <t-form ref="form" :data="formData"  labelWidth="140px">
      <t-form-item label="是否通过监督局评审">
        <t-radio-group
          clearable
          v-model="formData.jdjReviewStatus"
          :options="[
            { value: '1', label: '是' },
            { value: '0', label: '否' },
          ]"
        >
        </t-radio-group>
      </t-form-item>
      <t-form-item v-if="formData.jdjReviewStatus === '1'" :label="dateLabel">
        <t-date-picker
          clearable
          v-model="formData.replyDate"
        ></t-date-picker>
      </t-form-item>
      <t-form-item v-if="formData.jdjReviewStatus === '1'" :label="fileLabel" name="projectFileData">
          <file-upload v-model="projectFileData.fileIds" :moduleKey="projectFileData.moduleKey"
                           :fileList="projectFileData.fileLists"
                           :multiple="true">
                <t-button variant="outline">
                  <t-icon name="cloud-upload" slot="icon"/>
                  点击上传
                </t-button>
              </file-upload>
        </t-form-item>
    </t-form>
    </div>
    <t-dialog header="提示" placement="center" :visible.sync="tipVisible" :onConfirm="onConfirmAnother" :on-cancel="closeTipDialog" :onClose="closeTipDialog">
      <p>是否确认提交？</p>
    </t-dialog>
  </t-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue"
import {getFilesByModuleKey} from "@/api/common";


export default {
  components: {
    FileUpload
  },
  props: {
    header: String,
    objId: [String, Number],
    id: [String, Number],
    localId: [String, Number],
    xsReviewType: [String, Number],
    moduleKey: [String]
  },
  data() {
    return {
      visible: false,
      tipVisible: false,
      dateLabel: '',
      fileLabel: '',
      projectFileData: {
        id: 1,
        c1: "批复立项文件",
        c2: "否",
        fileIds: [],
        fileLists: [],
        moduleKey: "",
        require: false
      },
      formData: {
        jdjReviewStatus: '1',
        replyDate: '',
      },
    };
  },
  created() {
    this.projectFileData.moduleKey = this.moduleKey
    let request = null
    let paramId = this.id
    if (this.xsReviewType === "1") {
      this.dateLabel = '批复立项时间'
      this.fileLabel = '批复立项文件'
      request = this.$api.bzh.standard.getApply
    } else if (this.xsReviewType === "2") {
      this.dateLabel = '批复报审时间'
      this.fileLabel = '批复报审文件'
      request = this.$api.bzh.report.getBySuggestId
      paramId = this.objId
    } else if (this.xsReviewType === "3") {
      this.dateLabel = '批复报批时间'
      this.fileLabel = '批复报批文件'
      request = this.$api.bzh.submit.getBySuggestId
      paramId = this.objId
    }
    request(paramId).then(({data: {replyDate}}) => {
      this.formData.replyDate = replyDate || ''
    })
    getFilesByModuleKey(this.id, this.projectFileData.moduleKey).then((res) => {
      const files = []
      res.data.forEach((item) => {
        if (!files[item.moduleKey]) {
          files[item.moduleKey] = []
        }
        files.push({
          name: item.fileName,
          url: item.fullFilePath,
          id: item.id,
          filePath: item.filePath
        })
      })
      this.projectFileData.fileLists = files || [];
      this.projectFileData.fileIds = files?.map(val => val.id) || [];
      this.visible = true;
    }).catch((e) => {
      this.$message.error(e.toString());
    });

  },
  methods: {
    confirmTip() {
      this.tipVisible = true
    },
    onConfirmAnother() {
      const deepForm = JSON.parse(JSON.stringify(this.formData))
      let sysAttMainIds = []
      if (this.formData.jdjReviewStatus === '1') {
        sysAttMainIds = [...this.projectFileData.fileIds]
      } else {
        delete deepForm.replyDate
      }
      deepForm.id = this.id;
      deepForm.localId = this.objId
      deepForm.xsReviewType = this.xsReviewType;
      let request = null
      if (this.xsReviewType === "1") {
        request = this.$api.bzh.standard.addReply
      } else if (this.xsReviewType === "2") {
        request = this.$api.bzh.standard.reportTrialAddReply
      } else if (this.xsReviewType === "3") {
        request = this.$api.bzh.standard.submitApprovalAddReply
      }
      request({
        ...deepForm,
        sysAttMainVo: {
          sysAttMainIds
        }
      }).then(res => {
        this.$message.success(res.msg);
        this.$emit("update")
        this.$emit('update:visible', false);
      }).catch(e => {
        this.$message.error(e.toString())
      })
    },
    closeDialog() {
      this.$emit('update:visible', false);
    },
    closeTipDialog() {
      this.$emit('update:tipVisible', false);
    },
  },
};
</script>
